package org.jbit.news.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jbit.news.biz.NewsBiz;
import org.jbit.news.biz.TopicBiz;
import org.jbit.news.biz.impl.NewsBizImpl;
import org.jbit.news.biz.impl.TopicBizImpl;
import org.jbit.news.entity.Topic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class TopicServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String path = request.getContextPath();
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String opr = request.getParameter("opr");
		
		String tname = request.getParameter("tname");
		String tid = request.getParameter("tid");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Topic topic = new Topic();

		TopicBiz topicBiz = new TopicBizImpl();
		List<Topic> listg = topicBiz.getAllTopics();
if (opr==null) {
	opr="ss";
		};
		if (opr=="ss") {
			
			String topicJson=JSON.toJSONString(listg,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullStringAsEmpty);
			out.print(topicJson);
		}
		 
		/*StringBuffer topicJson=new StringBuffer("[");
		for (int i = 0; i < listg.size();) {
			topic=listg.get(i);
			topicJson.append("{\"tname\":\""+topic.getTname().replace("\"", "")+"\"}");
			if (++i==listg.size()) {
				break;
			}else {
				topicJson.append(",");
			}
		}
		topicJson.append("]");*/
		
		
		
		

		NewsBiz newsBiz = new NewsBizImpl();
		
		//session.setAttribute("listTb", listg);
		if (opr.equals("add")) {
		/*	List<Topic> list = topicBiz.fingTopicByName(tname);

			session.setAttribute("list", list);*/
//			if (list.size() < 1) {
//				int num = topicBiz.addToipc(tname);
//				request.getRequestDispatcher("../newspages/topic_list.jsp")
//						.forward(request, response);
//
//			} else {
//				response.sendRedirect("../newspages/topic_list.jsp");
//			}
			//ajax添加主题
			 String status;  //记录执行结果
			 String  message;  //记录提示信息
			 int result = topicBiz.addToipc(tname);
			 if (result ==-1) {
				 status="exist";
					message="当前主题已存在，请输入不同的主题！";
				
				System.out.println(1);
			}else {
				status="success";
				message="主题创建成功";
			}
				out.print("[{\"status\":\""+status+"\",\"message\":\""+message+"\"}]");
			
			

		} else if (opr.equals("delTopic")) {

			int num = topicBiz.deleteTopic(Integer.parseInt(tid));
			if (num > 0) { 
				request.getRequestDispatcher("../newspages/topic_list.jsp")
						.forward(request, response);
			} else {
				response.sendRedirect("../newspages/topic_list.jsp");
			}
		} else if (opr.equals("update")) {
			
			topic.setTname(tname);
			topic.setTid(Integer.parseInt(tid));
			String  status;
			String  message;
			int i = topicBiz.updateTopic(topic);
			if (i ==-1) {
				status="exist";
				message="当前主题已存在，请输入不同的主题！";
				/*response.sendRedirect("../newspages/topic_list.jsp");*/
			} else if(i==0){
				status="error";
				message="未找到相关主题！";
			/*	response.sendRedirect("../newspages/topic_list.jsp");*/
			}else {
				status="success";
				message="已成功更新主题！";
			}
			out.print("[{\"status\":\""+status+"\",\"message\":\""+message+"\"}]");
          System.out.println("[{\"status\":\""+status+"\",\"message\":\""+message+"\"}]");
		}
		out.flush();
		out.close();

	}
}
